%{

#include <string.h>
#include <math.h> 
#include <stdio.h>
#include "parser.tab.h"
#include "automata.h"

int line = 1, col = 1;            /* current position in line, column */
extern int hashcount;             /* current legal entry in hashtable */
extern char hashtable[MAXHASHSIZE][MAXHASHENTRYLEN]; /* hashtable for identifiers */
int addtohash(char* s);           /* add an identifier to hashtable*


%}

%%

 

[ \t]   { col += (int) strlen(yytext); }   /* ignore but count white space */


"F"     {col += (int) strlen(yytext); return FUTURE;  }
"G"     {col += (int) strlen(yytext); return GLOBAL; }
"X"     {col += (int) strlen(yytext); return NEXT;    }
"U"     {col += (int) strlen(yytext); return UNTIL;   }


"AND"   {col += (int) strlen(yytext); return AND;  }
"OR"    {col += (int) strlen(yytext); return OR;  }
"NOT"   {col += (int) strlen(yytext); return NOT;    }


"("     {col += (int) strlen(yytext); return LBR;     }
")"     {col += (int) strlen(yytext); return RBR;     }
"["     {col += (int) strlen(yytext); return LSQBR;  } 
"]"     {col += (int) strlen(yytext); return RSQBR;  } 
"\{"    {col += (int) strlen(yytext); return LCBR;  }
"\}"    {col += (int) strlen(yytext); return RCBR;  } 


"*"     {col += (int) strlen(yytext); return MUL; }
"/"     {col += (int) strlen(yytext); return DIV; }
"+"     {col += (int) strlen(yytext); return ADD; }
"-"     {col += (int) strlen(yytext); return SUB; }


            /* variables */
[A-Za-z][A-Za-z0-9_]*     { col += (int) strlen(yytext);   yylval = addtohash(yytext); return (IDENTIFIER);}


            /* real number returned as type INTEGER */
[0-9][0-9]*\.[0-9][0-9]* {  col += (int) strlen(yytext);   yylval = addtohash(yytext); return (INTEGER);}

            /* integer */ 
[0-9][0-9]*              {  col += (int) strlen(yytext);  yylval = addtohash(yytext); return (INTEGER);}

            /* Comparators */

">"    { col += (int) strlen(yytext);  yylval = addtohash(yytext); return (COMPARATOR);}
"<"    { col += (int) strlen(yytext);  yylval = addtohash(yytext); return (COMPARATOR);}
">="   { col += (int) strlen(yytext);  yylval = addtohash(yytext); return (COMPARATOR);}
"<="   { col += (int) strlen(yytext);  yylval = addtohash(yytext); return (COMPARATOR);}
"="    { col += (int) strlen(yytext);  yylval = addtohash(yytext); return (COMPARATOR);}



\n      { col = 0; ++line; return 0; }

.       { col += (int) strlen(yytext); return yytext[0]; }






%%




/*
Checks if the MAXHASHENTRYLEN length string pointed to by s lies in the hastable
Creates a new entry at the end of the table otherwise
hashcount is a global counter; starts at 0
hashtable[hashcount] is entry waiting to be written
Returns the index of the string in the hashtable anyway
*/

int addtohash(char* s)
{
int i;
#ifndef PERFORMANCE
printf("\n Entered addtohash for Identifiers\n");
#endif

/* Search for the variable in the hashtable*/
for(i=0;i<hashcount;i++)
{
if(!strcmp(s,hashtable[i])) return i;
}

#ifndef PERFORMANCE
printf("\n Adding new entry %s to hashtable at %d\n",s,hashcount);
#endif

/* Copies the first 64 characters of the string into the hashtable */
hashcount++;
strncpy(hashtable[hashcount],s,MAXHASHENTRYLEN);



#ifndef PERFORMANCE
printf("\n Left addtohash for Identifiers\n");
#endif

return (hashcount);
}





/*
Reset the error location tracking variables
*/

void reset_lexer(void)
{

  line = 1;
  col  = 1;

}



/**
 * yyerror() is invoked when the lexer or the parser encounter
 * an error. The error message is passed via *s
 *
 *
 */
void yyerror(char *s)
{
  printf("error: %s at line: %d col: %d\n",s,line,col);

}



int yywrap(void)
{
  return 1;
}

